package com.rickpan.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.rickpan.entity.GeneratedReport;
import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 报告管理相关响应DTO
 */
public class ReportManageResponse {

    /**
     * 报告基本信息响应
     */
    @Schema(description = "报告基本信息")
    public static class ReportSummaryResponse {
        
        @Schema(description = "报告ID", example = "1")
        private Long id;
        
        @Schema(description = "报告标题", example = "2025-01-10 工作日报")
        private String title;
        
        @Schema(description = "报告类型", example = "DAILY")
        private GeneratedReport.ReportType type;
        
        @Schema(description = "报告类型显示名称", example = "日报")
        private String typeDisplayName;
        
        @Schema(description = "时间范围开始日期", example = "2025-01-10")
        @JsonFormat(pattern = "yyyy-MM-dd")
        private LocalDate periodStart;
        
        @Schema(description = "时间范围结束日期", example = "2025-01-10")
        @JsonFormat(pattern = "yyyy-MM-dd")
        private LocalDate periodEnd;
        
        @Schema(description = "报告状态", example = "FINAL")
        private GeneratedReport.ReportStatus status;
        
        @Schema(description = "报告状态显示名称", example = "最终版")
        private String statusDisplayName;
        
        @Schema(description = "版本号", example = "1")
        private Integer version;
        
        @Schema(description = "是否已编辑", example = "true")
        private Boolean isEdited;
        
        @Schema(description = "报告标签")
        private List<String> tags;
        
        @Schema(description = "创建时间", example = "2025-01-10 09:00:00")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime createdAt;
        
        @Schema(description = "更新时间", example = "2025-01-10 10:30:00")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime updatedAt;
        
        @Schema(description = "内容预览（前100字符）")
        private String contentPreview;

        // 构造函数
        public ReportSummaryResponse() {}

        public ReportSummaryResponse(GeneratedReport report) {
            this.id = report.getId();
            this.title = report.getTitle();
            this.type = report.getType();
            this.typeDisplayName = report.getType().getDisplayName();
            this.periodStart = report.getPeriodStart();
            this.periodEnd = report.getPeriodEnd();
            this.status = report.getStatus();
            this.statusDisplayName = report.getStatus().getDisplayName();
            this.version = report.getVersion();
            this.isEdited = report.isEdited();
            this.createdAt = report.getCreatedAt();
            this.updatedAt = report.getUpdatedAt();
            
            // 生成内容预览
            String content = report.getCurrentContent();
            this.contentPreview = content != null && content.length() > 100 
                                ? content.substring(0, 100) + "..." 
                                : content;
        }

        // Getter和Setter方法
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }

        public String getTitle() { return title; }
        public void setTitle(String title) { this.title = title; }

        public GeneratedReport.ReportType getType() { return type; }
        public void setType(GeneratedReport.ReportType type) { this.type = type; }

        public String getTypeDisplayName() { return typeDisplayName; }
        public void setTypeDisplayName(String typeDisplayName) { this.typeDisplayName = typeDisplayName; }

        public LocalDate getPeriodStart() { return periodStart; }
        public void setPeriodStart(LocalDate periodStart) { this.periodStart = periodStart; }

        public LocalDate getPeriodEnd() { return periodEnd; }
        public void setPeriodEnd(LocalDate periodEnd) { this.periodEnd = periodEnd; }

        public GeneratedReport.ReportStatus getStatus() { return status; }
        public void setStatus(GeneratedReport.ReportStatus status) { this.status = status; }

        public String getStatusDisplayName() { return statusDisplayName; }
        public void setStatusDisplayName(String statusDisplayName) { this.statusDisplayName = statusDisplayName; }

        public Integer getVersion() { return version; }
        public void setVersion(Integer version) { this.version = version; }

        public Boolean getIsEdited() { return isEdited; }
        public void setIsEdited(Boolean isEdited) { this.isEdited = isEdited; }

        public List<String> getTags() { return tags; }
        public void setTags(List<String> tags) { this.tags = tags; }

        public LocalDateTime getCreatedAt() { return createdAt; }
        public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }

        public LocalDateTime getUpdatedAt() { return updatedAt; }
        public void setUpdatedAt(LocalDateTime updatedAt) { this.updatedAt = updatedAt; }

        public String getContentPreview() { return contentPreview; }
        public void setContentPreview(String contentPreview) { this.contentPreview = contentPreview; }
    }

    /**
     * 报告详细信息响应
     */
    @Schema(description = "报告详细信息")
    public static class ReportDetailResponse extends ReportSummaryResponse {
        
        @Schema(description = "AI原始生成内容")
        private String originalContent;
        
        @Schema(description = "用户编辑后内容")
        private String editedContent;
        
        @Schema(description = "当前使用的内容")
        private String currentContent;

        // 构造函数
        public ReportDetailResponse() {}

        public ReportDetailResponse(GeneratedReport report) {
            super(report);
            this.originalContent = report.getOriginalContent();
            this.editedContent = report.getEditedContent();
            this.currentContent = report.getCurrentContent();
        }

        // Getter和Setter方法
        public String getOriginalContent() { return originalContent; }
        public void setOriginalContent(String originalContent) { this.originalContent = originalContent; }

        public String getEditedContent() { return editedContent; }
        public void setEditedContent(String editedContent) { this.editedContent = editedContent; }

        public String getCurrentContent() { return currentContent; }
        public void setCurrentContent(String currentContent) { this.currentContent = currentContent; }
    }

    /**
     * 报告统计信息响应
     */
    @Schema(description = "报告统计信息")
    public static class ReportStatsResponse {
        
        @Schema(description = "总报告数", example = "25")
        private Long totalReports;
        
        @Schema(description = "各类型报告数量统计")
        private Map<String, Long> typeStats;
        
        @Schema(description = "各状态报告数量统计")
        private Map<String, Long> statusStats;
        
        @Schema(description = "最近7天报告创建趋势")
        private List<DailyStatsItem> recentTrend;
        
        @Schema(description = "月度报告创建趋势")
        private List<MonthlyStatsItem> monthlyTrend;

        // 内部类：日统计项
        public static class DailyStatsItem {
            @JsonFormat(pattern = "yyyy-MM-dd")
            private LocalDate date;
            private Long count;

            public DailyStatsItem(LocalDate date, Long count) {
                this.date = date;
                this.count = count;
            }

            public LocalDate getDate() { return date; }
            public void setDate(LocalDate date) { this.date = date; }

            public Long getCount() { return count; }
            public void setCount(Long count) { this.count = count; }
        }

        // 内部类：月统计项
        public static class MonthlyStatsItem {
            private Integer year;
            private Integer month;
            private Long count;

            public MonthlyStatsItem(Integer year, Integer month, Long count) {
                this.year = year;
                this.month = month;
                this.count = count;
            }

            public Integer getYear() { return year; }
            public void setYear(Integer year) { this.year = year; }

            public Integer getMonth() { return month; }
            public void setMonth(Integer month) { this.month = month; }

            public Long getCount() { return count; }
            public void setCount(Long count) { this.count = count; }
        }

        // Getter和Setter方法
        public Long getTotalReports() { return totalReports; }
        public void setTotalReports(Long totalReports) { this.totalReports = totalReports; }

        public Map<String, Long> getTypeStats() { return typeStats; }
        public void setTypeStats(Map<String, Long> typeStats) { this.typeStats = typeStats; }

        public Map<String, Long> getStatusStats() { return statusStats; }
        public void setStatusStats(Map<String, Long> statusStats) { this.statusStats = statusStats; }

        public List<DailyStatsItem> getRecentTrend() { return recentTrend; }
        public void setRecentTrend(List<DailyStatsItem> recentTrend) { this.recentTrend = recentTrend; }

        public List<MonthlyStatsItem> getMonthlyTrend() { return monthlyTrend; }
        public void setMonthlyTrend(List<MonthlyStatsItem> monthlyTrend) { this.monthlyTrend = monthlyTrend; }
    }
}
